package com.foreknow.test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        //创建一个Map容器
        Map<String,String> map = new HashMap<>();
        //向Map中添加元素
        map.put("1","fender");
        map.put("2","gibson");
        map.put("3","jackson");
        //从Map中根据key来获取到某个元素
        String name = map.get("2");
        System.out.println(name);
        //如何对Map集合进行遍历
        System.out.println("------------------------遍历Map集合-------------------------");
        Set<Map.Entry<String,String>> set = map.entrySet();
        Iterator<Map.Entry<String,String>> it = set.iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            System.out.println(entry.getKey() + "---" + entry.getValue());
        }
    }
}
